package com.osell.selection.ui.common;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.osell.selection.ui.base.BaseDialogFragment;
import com.osell.selection.ui.main.MainActivity;
import com.osell.selection.util.LocaleHelper;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class LanguageFragment extends BaseDialogFragment implements View.OnClickListener {

    @BindView(R.id.tv_english)
    TextView tv_english;
    @BindView(R.id.tv_simplified_chinese)
    TextView tv_simplified_chinese;

    private Unbinder mUnBinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_language, container, false);
        mUnBinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        if (TextUtils.equals(Locale.ENGLISH.getLanguage(), LocaleHelper.getLanguage(view.getContext()))) {
            tv_english.setTextColor(ContextCompat.getColor(view.getContext(), R.color.colorPrimary));
        } else {
            tv_simplified_chinese.setTextColor(ContextCompat.getColor(view.getContext(), R.color.colorPrimary));
        }
        tv_english.setOnClickListener(this);
        tv_simplified_chinese.setOnClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_english:
                if (!TextUtils.equals(Locale.ENGLISH.getLanguage(), LocaleHelper.getLanguage(v.getContext()))) {
                    LocaleHelper.setLocale(v.getContext(), Locale.ENGLISH.getLanguage());
                    startActivity(new Intent(v.getContext(), MainActivity.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
                }
                dismiss();
                break;
            case R.id.tv_simplified_chinese:
                if (TextUtils.equals(Locale.ENGLISH.getLanguage(), LocaleHelper.getLanguage(v.getContext()))) {
                    LocaleHelper.setLocale(v.getContext(), Locale.SIMPLIFIED_CHINESE.getLanguage());
                    startActivity(new Intent(v.getContext(), MainActivity.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
                }
                dismiss();
                break;
            default:
                break;
        }
    }
}
